package zgrjb.snbq.teach_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zgrjb.snbq.teach_platform.dto.model.CommentDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zgrjb.snbq.teach_platform.dto.model.CommentDO;
import zgrjb.snbq.teach_platform.dto.model.TopicDO;
import zgrjb.snbq.teach_platform.dto.model.User;
import zgrjb.snbq.teach_platform.dto.req.AddCommentRequest;
import zgrjb.snbq.teach_platform.interceptor.LoginInterceptor;
import zgrjb.snbq.teach_platform.mapper.CommentMapper;
import zgrjb.snbq.teach_platform.service.CommentService;
import zgrjb.snbq.teach_platform.util.IDUtil;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.LocalDateTime;

@Service
public class CommentServiceImpl implements CommentService {


    @Autowired
    private CommentMapper commentMapper;

    @Override
    public JsonData listComment(Long topicId) {
        List<CommentDO> list = commentMapper.selectList(new QueryWrapper<CommentDO>()
                .eq("topic_id",topicId)
                .orderByDesc("time"));
        return JsonData.buildSuccess(list);
    }

    @Override
    public JsonData addComment(CommentDO commentDO) {
        User user = LoginInterceptor.getCurrentUser();
        commentDO.setId(((Long) IDUtil.geneSnowFlakeID()));
        commentDO.setTime(LocalDateTime.now());
        commentDO.setStudentId(user.getId());
        commentDO.setAuthor(user.getName());
        commentMapper.insert(commentDO);
        return JsonData.buildSuccess();
    }



    @Override
    public JsonData add(AddCommentRequest request) {
        CommentDO commentDO = new CommentDO();
        User user = LoginInterceptor.getCurrentUser();
        commentDO.setId((Long) IDUtil.geneSnowFlakeID());
        commentDO.setTopicId(request.getTopicId());
        commentDO.setMessage(request.getMessage());
        commentDO.setTime(LocalDateTime.now());
        commentDO.setStudentId(user.getId());
        commentDO.setAuthor(user.getName());
        commentMapper.insert(commentDO);
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData goodComment(Long commentId) {
        CommentDO commentDO = commentMapper.selectById(commentId);
        commentDO.setGood(commentDO.getGood()+1);
        commentMapper.updateById(commentDO);
        return JsonData.buildSuccess();

    }

    @Override
    public JsonData list(Long topicId,int page,int size) {

        User loginUser = LoginInterceptor.getCurrentUser();

        Page<CommentDO> topicPage = new Page<>(page, size);
        Page<CommentDO> topicPageRes = commentMapper.selectPage(topicPage, new QueryWrapper<CommentDO>()
                .eq("topic_id", topicId)
                .orderByDesc("time"));
        return JsonData.buildSuccess(topicPageRes);
    }

    @Override
    public JsonData del(Long commentId) {
        User user = LoginInterceptor.getCurrentUser();
        int row = commentMapper.delete(new QueryWrapper<CommentDO>()
                .eq("id",commentId)
                .eq("student_id",user.getId()));
        return row > 0 ? JsonData.buildSuccess() : JsonData.buildError("删除失败");
    }
}
